package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.MD5Util;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.appuser.entity.TbUserAlipay;
import org.jeecg.modules.appuser.entity.TbUserAlipayPwd;
import org.jeecg.modules.appuser.mapper.TbUserAlipayPwdMapper;
import org.jeecg.modules.appuser.service.ITbUserAlipayPwdService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 支付密码
 * @Author: jeecg-boot
 * @Date: 2020-06-28
 * @Version: V1.0
 */
@Service
public class TbUserAlipayPwdServiceImpl extends ServiceImpl<TbUserAlipayPwdMapper, TbUserAlipayPwd> implements ITbUserAlipayPwdService {

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    @Override
    public Result<?> setAlipayPwd(TbUserAlipayPwd tbUserAlipayPwd) {

        if (null == tbUserAlipayPwd || StringUtils.isEmpty(tbUserAlipayPwd.getUserId())) {

            return Result.error("参数错误");
        }

        String payPassword = tbUserAlipayPwd.getPayPassword();
        String real_pwd = MD5Util.MD5Encode(payPassword, "utf-8");
        tbUserAlipayPwd.setPayPassword(real_pwd);
        tbUserAlipayPwd.setStatus(1);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",tbUserAlipayPwd.getUserId());

        TbUserAlipayPwd  old_tbUserAlipayPwd= this.getOne(queryWrapper);
        if(null!=old_tbUserAlipayPwd){
            old_tbUserAlipayPwd.setPayPassword(real_pwd);
            this.updateById(old_tbUserAlipayPwd);
            return Result.ok("修改成功");

        }else{
            if (this.save(tbUserAlipayPwd)) {
                TbMnUser tbMnUser = tbMnUserService.getById(tbUserAlipayPwd.getUserId());
                if (null != tbMnUser) {
                    tbMnUser.setHasPayPwd(1);
                    tbMnUserService.updateById(tbMnUser);
                }
            }
            return Result.ok("设置成功");
        }


    }

    @Override
    public Result<?> payPwdConfirm(String user_id, String password) {

        String real_pwd = MD5Util.MD5Encode(password, "utf-8");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("pay_password",real_pwd);
        queryWrapper.eq("user_id",user_id);
        TbUserAlipayPwd tbUserAlipayPwd= this.getOne(queryWrapper);
        if(null!=tbUserAlipayPwd){
            return Result.ok("验证通过");
        }


        return Result.error("验证失败");
    }
}
